package com.yjhb.android.sbz.activity;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.view.View;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.BalanceAdapter;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.entity.BalanceDetailed;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.mvp.money.MoneyPresenter;
import com.yjhb.android.sbz.mvp.money.MoneyView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.LoadMoreListView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/2 17:01
 */
public class MoneyRewardActivity extends BaseActivity<MoneyPresenter> implements OnRefreshLoadmoreListener, MoneyView {
    private final String TAG = "MoneyRewardActivity";


    @BindView(R.id.listView)
    LoadMoreListView listView;
    @BindView(R.id.layout_empty)
    View layout_empty;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.balance_tv)
    TextView balance_tv;

    BalanceAdapter adapter;
    List<BalanceDetailed> balanceDetaileds = new ArrayList<>();

    private final int size = 25;
    private int current = 1;

    @Override
    protected boolean isCheckLogin() {
        return true;
    }

    @Override
    protected MoneyPresenter initPresenter() {
        return new MoneyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_money_reward;
    }

    @Override
    protected void initUI() {
        listView.setEmptyView(layout_empty);
        listView.setDividerHeight(0);
        listView.getLoadingProgress().setIndeterminateTintList(new ColorStateList(new int[][]{},
                new int[]{getResources().getColor(R.color.colorPrimaryDark)}));
        listView.setAdapter(adapter = new BalanceAdapter(this));
        adapter.setData(balanceDetaileds);
    }

    @Override
    protected void initData() {
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE,null));
    }

    @Override
    protected void regEvent() {
        refreshLayout.setOnRefreshLoadmoreListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsg(EventMsg msg) {
        if (msg.msg == EventMsg.USER_UPDATE_OK) {
            refresh();
        }
    }

    public void refresh() {
        balance_tv.setText(App.userInfo.getPayMoney());
        current = 1;
        presenter.getPayBillHistory(size, current);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        presenter.getPayBillHistory(size, ++current);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        current = 1;
        presenter.getPayBillHistory(size, current);
    }


    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @OnClick(R.id.recharge_layout)
    public void recharge_layout() {
        Tool.startRechargeActivity(this, 2, null, true);
    }

    @OnClick(R.id.withdrawal_layout)
    public void withdrawal_layout() {
        Intent intent = new Intent(this, WithdrawalActivity.class);
        intent.putExtra("type", 2);
        startActivity(intent);
    }

    @Override
    public void onFailure(int code, String msg) {
        ZxToast.showBottomToastLight("出错了哦~~");
    }

    @Override
    public void onLoad(List<BalanceDetailed> data) {
        this.balanceDetaileds.addAll(data);
        refreshLayout.finishLoadmore();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh(List<BalanceDetailed> data) {
        this.balanceDetaileds.clear();
        this.balanceDetaileds.addAll(data);
        refreshLayout.finishRefresh();
        adapter.notifyDataSetChanged();
    }
}
